package com.doucat.doucatmath.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.CornerPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
/**
 * -----------------------------------------------------------------
 * Copyright (C) 2021-2031, by tonly, All rights reserved.
 * -----------------------------------------------------------------
 * File: FxImageView.java
 * Author: wangfy
 * Create: 2021/7/28 11:19
 */
public class FxImageView extends View {
    private String TAG=getClass().getSimpleName();
    public FxImageView(Context context) {
        super(context);
        init();
    }

    public FxImageView(Context context,  AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public FxImageView(Context context,  AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public FxImageView(Context context,  AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }
    //坐标原点x
    private int originX;
    //坐标原点y
    private int originY;
    //坐标系画笔
    private Paint paintOrigin;
    private int paintOriginColor=Color.BLACK;
    private int bgColor=Color.WHITE;

    //刻度画笔
    private Paint paintScale;
    private int ScaleWidth=10;
    //坐标值画笔
    private Paint paintText;
    private int paintTextColor=Color.BLACK;
    private Paint paintFx;
    //定义域
    private double[] D=new double[]{-6,6};
    //描点的精度
    private double precision=0.1;
    public void setPrecision(double precision) {
        this.precision = precision;
    }

    //每100个像素宽度，代表数值1
    private int valuePx=100;
    private void init(){
        //初始化坐标画笔
        paintOrigin=new Paint();
        paintOrigin.setColor(paintOriginColor);
        //设置为实现
        paintOrigin.setStyle(Paint.Style.STROKE);
        //设置抗锯齿
        paintOrigin.setAntiAlias(true);
        //设置线宽
        paintOrigin.setStrokeWidth(5);

        //初始化坐标值刻度画笔
        paintScale=new Paint();
        paintScale.setColor(paintTextColor);
        //设置为实现
        paintScale.setStyle(Paint.Style.STROKE);
        //设置抗锯齿
        paintScale.setAntiAlias(true);
        //设置线宽
//        paintScale.setStrokeWidth(5);
        paintScale.setTextSize(12f);

        //初始化坐标值画笔
        paintText=new Paint();
        paintText.setColor(paintTextColor);
        //设置为实现
        paintText.setStyle(Paint.Style.STROKE);
        //设置抗锯齿
        paintText.setAntiAlias(true);
        //设置线宽
//        paintText.setStrokeWidth(5);
        paintText.setTextSize(12f);

        //初始化函数图像画笔
        paintFx =new Paint();
        paintFx.setColor(paintOriginColor);
        //设置为实现
        paintFx.setStyle(Paint.Style.STROKE);
        //设置抗锯齿
        paintFx.setAntiAlias(true);
        //设置线宽
        paintFx.setStrokeWidth(5);
        //设置线帽样式
        paintFx.setStrokeCap(Paint.Cap.ROUND);
        //设置线段连接处样式 各段Path设置不同的连接方式
        paintFx.setStrokeJoin(Paint.Join.ROUND);
        //将原来Path生硬的直线拐角，变成圆形拐角
        paintFx.setPathEffect(new CornerPathEffect(6));
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width=getWidth();
        int height=getHeight();
        canvas.drawColor(bgColor);

        //坐标原点取中间
        originX=width/2;
        originY=height/2;

        //把当前画布的原点移到xy
        canvas.translate(originX, originY);

        Path pathX = new Path();
        //移动到原点
        pathX.moveTo(-width/2,0);
        pathX.lineTo(width,0);
        //绘制x轴
        canvas.drawPath(pathX,paintOrigin);

        //绘制y
        Path pathY = new Path();
        pathY.moveTo(0,height/2);
        pathY.lineTo(0,-height/2);
        canvas.drawPath(pathY,paintOrigin);

        //绘制函数
        Path pathFx = new Path();
        //根据定义域绘制坐标值
        for(double x=D[0];x<D[1];x= (x+1)){
            //画x刻度
            canvas.drawLine((float) (x*valuePx), 0, (float) (x*valuePx), ScaleWidth, paintScale);
            //画x刻度文字 文字向下偏移30
            canvas.drawText(x+"",(float) (x*valuePx), 30,paintText);
            //画y刻度
            canvas.drawLine(0, (float) (x*valuePx), ScaleWidth, (float) (x*valuePx), paintScale);
            //画y刻度文字 文字向左偏移30
            canvas.drawText(x+"",-30, (float) (-x*valuePx),paintText);
        }

        //根据定义域的起点，设置绘制起点，设置时将起点转化为像素
        pathFx.moveTo((float) (D[0]*valuePx), (float) fx.fx(D[0])*valuePx);
        for(double x=D[0];x<D[1];x= (x+precision)){
            double y=fx.fx(x);
            Log.v(TAG,"x="+x+" y="+y);
            //对x,进行放大
            pathFx.lineTo((float) (x*valuePx), (float) -(y*valuePx));
        }
        canvas.drawPath(pathFx,paintFx);
    }

    private float downX;
    private float downY;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                downX = event.getX();
                downY = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                setTranslationX(getX() + (event.getX() - downX));
                setTranslationY(getY() + (event.getY() - downY));
                break;
        }
        return true;
    }

    //默认函数
    private Fx fx=new Fx() {
        @Override
        public double fx(double x) {
            return  2*Math.sin(2* degToRad(x*60)+Math.PI/2)+1;
        }
    };
    private double degToRad(double deg) {
        return deg * Math.PI / 180;
    }
    //设置函数关系
    public void setFx(Fx fx) {
        this.fx=fx;
    }

    //设置定义域
    public void setD(double[] d) {
        D = d;
    }
}